const app = getApp();
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    trainList:[],
    current_page:1,
    train_type_id: 119,
    last_page:1,
    total:0,
    per_page:15,
    tabNav:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    // var train_type_id = options.train_type_id
    // this.setData({ train_type_id: train_type_id})
    this.getTrainListData()
  },
  getTrainListData: function () {
    let that = this;
    
    util.request(api.TrainList, { train_type_id: this.data.train_type_id, page: this.data.current_page}).then(res => {
      if (res.code === 200) {
        var oldtrainList = that.data.trainList
        var newtrainList = oldtrainList.concat(res.data.trainList.data)
        that.setData({
          trainList: newtrainList,
          current_page: res.data.trainList.current_page,
          last_page: res.data.trainList.last_page,
          per_page: res.data.trainList.per_page,
          total: res.data.trainList.total,
          tabNav: res.data.tabNav,
          selectedId: res.data.tabNav.selectedId
        });
        console.log(this.data.trainList)
      }
      wx.hideLoading();
    }).catch(() => {
      // wx.navigateTo({
      //   url: '/pages/auth/login/login',
      // })
      wx.hideLoading();
    });
  },
  tabChange: function(e){
    this.setData({
      trainList: [],
      current_page: 1,
      train_type_id: e.detail
    })
    console.log(this.data.tabNav)
    this.getTrainListData()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // this.setData({ current_page: this.data.current_page+1})
    // this.getTrainListData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    wx.showLoading({
      title: '加载中！',
      success: function () {
        that.setData({ current_page: parseInt(that.data.current_page) + 1 })
        that.getTrainListData();
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})